#!/usr/local/env python3
# coding: utf-8
# author: Mr.zhang
# data: 2020-04-29
# 多线程目标主机端口探测 并实现写入文件

"""
思路分析：
    1、接收参数  python ip  port dir+port\ip
"""

import optparse
import socket
import sys
import time

class PortScanner():
    
    def __init__(self, ip, port):
        self.ip = ip
        self.port = port
        # self.fileobj = fileobj 

    def run(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(0.1)
        ret = s.connect_ex((self.ip, self.port))
        if ret == 0:
            # self.fileobj.write(str(self.port) + '\n')
            # print('NISOPORTST'+ str(self.ip) + ' ' + str(self.port)+'NISOPORTED')
            exit(233)
        else:
            exit(1)
       
def main():
    target = sys.argv[1]
    port = sys.argv[2]
    # filepath = sys.argv[3] + 'port\{}'.format(target)
    # fileobj = open(r'{}'.format(filepath), 'w+')
  
    scanner = PortScanner(target, int(port))
    scanner.run()

    # 关闭文件
    # fileobj.close()


if __name__ == "__main__":
    main()